package com.irdstudio.efp.nls.service.impl;

import com.alibaba.fastjson.JSON;
import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.bean.SedSynCusManageVO;
import com.irdstudio.efp.nls.service.dao.NlsApplyInfoDao;
import com.irdstudio.efp.nls.service.domain.NlsApplyInfo;
import com.irdstudio.efp.nls.service.facade.NlsApplyInfoService;
import com.irdstudio.efp.nls.service.vo.NlsApplyInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 网贷申请基本信息
 * 
 * @author heyingfeng
 * @since 2018年12月17日 下午7:46:24
 * @version 1.0
 */
@Service("nlsApplyInfoService")
public class NlsApplyInfoServiceImpl implements NlsApplyInfoService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(NlsApplyInfoServiceImpl.class);
	private static final String CHANGE_DATA_ERROR = "数据转换出现异常，异常信息为：";
	@Autowired
	private NlsApplyInfoDao nlsApplyInfoDao;

	/**
	 * 新增操作
	 * 
	 * @throws Exception
	 */
	@Override
	public int insertNlsApplyInfo(NlsApplyInfoVO inNlsApplyInfoVo) throws Exception {
		logger.debug("当前新增数据为:" + inNlsApplyInfoVo.toString());
		int num = 0;
		try {
			NlsApplyInfo nlsApplyInfo = new NlsApplyInfo();
			beanCopy(inNlsApplyInfoVo, nlsApplyInfo);
			num = nlsApplyInfoDao.insertNlsApplyInfo(nlsApplyInfo);
		} catch (DuplicateKeyException dke) {
			throw new Exception("新增数据发生异常，主键冲突!");
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
			throw e;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 删除操作
	 */
	@Override
	public int deleteByPk(NlsApplyInfoVO inNlsApplyInfoVo) {
		logger.debug("当前删除数据条件为:" + inNlsApplyInfoVo);
		int num = 0;
		try {
			NlsApplyInfo nlsApplyInfo = new NlsApplyInfo();
			beanCopy(inNlsApplyInfoVo, nlsApplyInfo);
			num = nlsApplyInfoDao.deleteByPk(nlsApplyInfo);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inNlsApplyInfoVo + "删除的数据条数为" + num);
		return num;
	}

	/**
	 * 更新操作
	 */
	@Override
	public int updateByPk(NlsApplyInfoVO inNlsApplyInfoVo) throws Exception {
		logger.debug("当前修改数据为:" + inNlsApplyInfoVo.toString());
		int num = 0;
		try {
			NlsApplyInfo nlsApplyInfo = new NlsApplyInfo();
			beanCopy(inNlsApplyInfoVo, nlsApplyInfo);
			num = nlsApplyInfoDao.updateByPk(nlsApplyInfo);
		} catch (Exception e) {
			logger.error("", e);
			num = -1;
			throw e;
		}
		logger.debug("根据条件:" + inNlsApplyInfoVo + "修改的数据条数为" + num);
		return num;
	}

	/**
	 * 查询操作
	 */
	@Override
	public NlsApplyInfoVO queryByPk(NlsApplyInfoVO inNlsApplyInfoVo) {

		logger.debug("当前查询参数信息为:" + inNlsApplyInfoVo);
		try {
			NlsApplyInfo queryNlsApplyInfo = new NlsApplyInfo();
			beanCopy(inNlsApplyInfoVo, queryNlsApplyInfo);
			NlsApplyInfo queryRslNlsApplyInfo = nlsApplyInfoDao.queryByPk(queryNlsApplyInfo);
			if (Objects.nonNull(queryRslNlsApplyInfo)) {
				NlsApplyInfoVO outNlsApplyInfoVo = new NlsApplyInfoVO();
				beanCopy(queryRslNlsApplyInfo, outNlsApplyInfoVo);
				logger.debug("当前查询结果为:" + outNlsApplyInfoVo.toString());
				return outNlsApplyInfoVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}
	
	/**
	 * 根据流水号以及百度reqSn判断
	 */
	@Override
	public NlsApplyInfoVO queryCopy(NlsApplyInfoVO inNlsApplyInfoVo) {
		logger.debug("当前查询参数信息为:" + inNlsApplyInfoVo);
		try {
			NlsApplyInfo queryNlsApplyInfo = new NlsApplyInfo();
			beanCopy(inNlsApplyInfoVo, queryNlsApplyInfo);
			NlsApplyInfo queryRslNlsApplyInfo = nlsApplyInfoDao.queryCopy(queryNlsApplyInfo);
			if (Objects.nonNull(queryRslNlsApplyInfo)) {
				NlsApplyInfoVO outNlsApplyInfoVo = new NlsApplyInfoVO();
				beanCopy(queryRslNlsApplyInfo, outNlsApplyInfoVo);
				logger.debug("当前查询结果为:" + outNlsApplyInfoVo.toString());
				return outNlsApplyInfoVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 查询用户权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<NlsApplyInfoVO> queryAllOwner(NlsApplyInfoVO nlsApplyInfoVo) throws Exception {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<NlsApplyInfoVO> list = null;
		try {
			List<NlsApplyInfo> nlsApplyInfos = nlsApplyInfoDao.queryAllOwnerByPage(nlsApplyInfoVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + nlsApplyInfos.size());
			pageSet(nlsApplyInfos, nlsApplyInfoVo);
			list = (List<NlsApplyInfoVO>) beansCopy(nlsApplyInfos, NlsApplyInfoVO.class);
		} catch (Exception e) {
			logger.error(CHANGE_DATA_ERROR, e);
			throw e;
		}

		return list;

	}

	/**
	 * 查询当前机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<NlsApplyInfoVO> queryAllCurrOrg(NlsApplyInfoVO nlsApplyInfoVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<NlsApplyInfo> nlsApplyInfos = nlsApplyInfoDao.queryAllCurrOrgByPage(nlsApplyInfoVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:" + nlsApplyInfos.size());
		List<NlsApplyInfoVO> list = null;
		try {
			pageSet(nlsApplyInfos, nlsApplyInfoVo);
			list = (List<NlsApplyInfoVO>) beansCopy(nlsApplyInfos, NlsApplyInfoVO.class);
		} catch (Exception e) {
			logger.error(CHANGE_DATA_ERROR, e);
		}

		return list;

	}

	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<NlsApplyInfoVO> queryAllCurrDownOrg(NlsApplyInfoVO nlsApplyInfoVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<NlsApplyInfo> nlsApplyInfos = nlsApplyInfoDao.queryAllCurrDownOrgByPage(nlsApplyInfoVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + nlsApplyInfos.size());
		List<NlsApplyInfoVO> list = null;
		try {
			pageSet(nlsApplyInfos, nlsApplyInfoVo);
			list = (List<NlsApplyInfoVO>) beansCopy(nlsApplyInfos, NlsApplyInfoVO.class);
		} catch (Exception e) {
			logger.error(CHANGE_DATA_ERROR, e);
		}

		return list;

	}

	/**
	 * 查询当前产品种类权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<NlsApplyInfoVO> queryAllCurrOwnerPrd(NlsApplyInfoVO nlsApplyInfoVo) {

		logger.debug("当前查询本人所属产品权限数据信息的参数信息为:");
		List<NlsApplyInfo> nlsApplyInfos = nlsApplyInfoDao.queryAllCurrOwnerPrdByPage(nlsApplyInfoVo);
		logger.debug("当前查询本人所属产品数据信息的结果集数量为:" + nlsApplyInfos.size());
		List<NlsApplyInfoVO> list = null;
		try {
			pageSet(nlsApplyInfos, nlsApplyInfoVo);
			list = (List<NlsApplyInfoVO>) beansCopy(nlsApplyInfos, NlsApplyInfoVO.class);
		} catch (Exception e) {
			logger.error(CHANGE_DATA_ERROR, e);
		}

		return list;

	}
	/**
	 * 查询当前客户是否存在
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<NlsApplyInfoVO> queryCertCode(NlsApplyInfoVO nlsApplyInfoVo) {
		logger.debug("查询当前客户是否存在:");
		List<NlsApplyInfo> nlsApplyInfos = nlsApplyInfoDao.queryCertCode(nlsApplyInfoVo);
		logger.debug("查询当前客户是否存在:" + nlsApplyInfos.size());
		List<NlsApplyInfoVO> list = null;
		try {
			list = (List<NlsApplyInfoVO>) beansCopy(nlsApplyInfos, NlsApplyInfoVO.class);
		} catch (Exception e) {
			logger.error(CHANGE_DATA_ERROR, e);
		}
		return list;
	}

	/**
	 * 通过条件查询信息
	 * 
	 * @param nlsApplyInfoVo
	 * @return
	 * @author qiuyf
	 * @since 2019年1月17日 下午3:30:12
	 * @version 1.0
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<NlsApplyInfoVO> queryListByConditions(NlsApplyInfoVO nlsApplyInfoVo) {
		logger.debug("通过条件查询列表数据开始......");
		NlsApplyInfo nlsApplyInfo = beanCopy(nlsApplyInfoVo, new NlsApplyInfo());
		List<NlsApplyInfoVO> result = null;
		try {
			List<NlsApplyInfo> list = nlsApplyInfoDao.queryByConditionByPage(nlsApplyInfo);
			pageSet(list, nlsApplyInfo);
			result = (List<NlsApplyInfoVO>) beansCopy(list, NlsApplyInfoVO.class);
		} catch (Exception e) {
			logger.error(CHANGE_DATA_ERROR + e.getMessage());
		}
		logger.debug("通过条件查询列表数据结束----------");
		return result;
	}

	@Override
	public NlsApplyInfoVO qryOneByLmtApplSeq(NlsApplyInfoVO nlsApplyInfoVo) {
		logger.debug("当前查询参数信息为:" + nlsApplyInfoVo);
		try {
			NlsApplyInfo queryNlsApplyInfo = new NlsApplyInfo();
			beanCopy(nlsApplyInfoVo, queryNlsApplyInfo);
			NlsApplyInfo queryRslNlsApplyInfo = nlsApplyInfoDao.qryOneByLmtApplSeq(queryNlsApplyInfo);
			if (Objects.nonNull(queryRslNlsApplyInfo)) {
				NlsApplyInfoVO outNlsApplyInfoVo = new NlsApplyInfoVO();
				beanCopy(queryRslNlsApplyInfo, outNlsApplyInfoVo);
				logger.debug("当前查询结果为:" + outNlsApplyInfoVo.toString());
				return outNlsApplyInfoVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<NlsApplyInfoVO> qryListByConditionsTwo(NlsApplyInfoVO nlsApplyInfoVo) {
		logger.debug("通过条件查询列表数据开始......");
		NlsApplyInfo nlsApplyInfo = beanCopy(nlsApplyInfoVo, new NlsApplyInfo());
		List<NlsApplyInfoVO> result = null;
		try {
			List<NlsApplyInfo> list = null;
			if(nlsApplyInfo.getStartNum() > -1 && nlsApplyInfo.getPageSize() > 0) {
				list = nlsApplyInfoDao.queryByCondition2Page(nlsApplyInfo);
			}else {
				list = nlsApplyInfoDao.queryListByConditions(nlsApplyInfo);
			}
			result = (List<NlsApplyInfoVO>) beansCopy(list, NlsApplyInfoVO.class);
		} catch (Exception e) {
			logger.error(CHANGE_DATA_ERROR + e.getMessage());
		}
		logger.debug("通过条件查询列表数据结束----------");
		return result;
	}
	
	@Override
	public int queryTotalByCondition(NlsApplyInfoVO nlsApplyInfoVo) {
		logger.debug("通过条件查询数据总条数开始......入参为："+ JSON.toJSONString(nlsApplyInfoVo));
		int result = 0;
		try {
			NlsApplyInfo nlsApplyInfo = beanCopy(nlsApplyInfoVo, new NlsApplyInfo());
			result = nlsApplyInfoDao.queryTotalByCondition(nlsApplyInfo);
		} catch (Exception e) {
			logger.error(CHANGE_DATA_ERROR + e.getMessage());
		}
		logger.debug("通过条件查询数据总条数结束----------");
		return result;
	}

	@Override
	public int countCustHasUsingLetter(NlsApplyInfoVO nlsApplyInfoVo) {
		logger.debug("根据证件类型、证件号码统计某个客户在途用信申请信息个数开始，入参为："+JSON.toJSONString(nlsApplyInfoVo));
		int res = 0;
		try {
			NlsApplyInfo nlsApplyInfo = beanCopy(nlsApplyInfoVo, new NlsApplyInfo());
			res = nlsApplyInfoDao.countCustHasUsingLetter(nlsApplyInfo);
		} catch (Exception e) {
			logger.error("根据证件类型、证件号码统计某个客户在途用信申请信息个数出现异常!", e);
		} finally {
			logger.debug("根据证件类型、证件号码统计某个客户在途用信申请信息个数结束，个数为：" + res);
		}
		return res;
	}

	/**
	 * 查询【放款申请信息流水】中未获取百度照片的申请人: <br>
	 *
	 * 0.1:caoxin:2019/3/25 12:28: 查询【放款申请信息流水】中未获取百度照片的申请人: <br>
	 * @author caoxin
	 * @return
	 * @since 2019/3/25 12:28
	 * @date 2019/3/25 12:28
	 * @version 0.1
	 */

	@Override
	public List<NlsApplyInfoVO> queryListByBdIdPictureState() {
		logger.debug("查询【放款申请信息流水】中未获取百度照片的申请人列表数据开始......");
		List<NlsApplyInfoVO> result = null;
		try {
			List<NlsApplyInfo> list = nlsApplyInfoDao.queryListByBdIdPictureState();
			result = (List<NlsApplyInfoVO>) beansCopy(list, NlsApplyInfoVO.class);
		} catch (Exception e) {
			logger.error(CHANGE_DATA_ERROR + e.getMessage());
		}
		logger.debug("通过【放款申请信息流水】中未获取百度照片的申请人列表数据结束----------");
		return result;
	}

	/**
	 * 用信申请临时表数据到网贷申请基本信息表有数据则更新，无则新增 <br>
	 * 0.2: weijj:2019年5月7日 下午7:27:56:新增 <br>
	 * @author  weijj
	 * @since 2019年5月7日 下午7:27:56
	 * @date 2019年5月7日 下午7:27:56
	 * @version 0.1 初始为0.1,后续变更则修改
	 */
	@SuppressWarnings("unchecked")
	@Override
	public int insertOrUpdateNlsApplyInfo(List<NlsApplyInfoVO> list) {
		// TODO Auto-generated method stub
		LocalDate today = LocalDate.now();
		String msg1 = "【网贷申请基本信息明细表】数据同步开始..." + today;
		logger.info(msg1, "message{}");
		List<NlsApplyInfo> nlsApplyInfolList = new ArrayList<NlsApplyInfo>();
		int num = 0;
		try {
			nlsApplyInfolList = (List<NlsApplyInfo>) beansCopy(list, NlsApplyInfo.class);
			num = nlsApplyInfoDao.insertOrUpdateNlsApplyInfo(nlsApplyInfolList);
			String num_msg = "【网贷申请基本信息明细表】插入或更新数据处理结果:" + num;
			logger.info(num_msg, "message{}");
		} catch (Exception e) {
			String msg2 = "【网贷申请基本信息明细表】插入或更新数据出现异常:" + e;
			logger.error(msg2, "message{}");
			num = -1;
		}
		return num;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int batchUpdate(List<NlsApplyInfoVO> list) {
		// TODO Auto-generated method stub
		LocalDate today = LocalDate.now();
		String msg1 = "【网贷申请基本信息明细表nls_apply_info】批量更新开始..." + today;
		logger.info(msg1, "message{}");
		List<NlsApplyInfo> nlsApplyInfolList = new ArrayList<NlsApplyInfo>();
		int num = 0;
		try {
			nlsApplyInfolList = (List<NlsApplyInfo>) beansCopy(list, NlsApplyInfo.class);
			num = nlsApplyInfoDao.batchUpdate(nlsApplyInfolList);
			String num_msg = "【网贷申请基本信息明细表nls_apply_info】插入或更新数据处理结果:" + num;
			logger.info(num_msg, "message{}");
		} catch (Exception e) {
			String msg2 = "【网贷申请基本信息明细表nls_apply_info】插入或更新数据出现异常:" + e;
			logger.error(msg2, "message{}");
			num = -1;
		}
		return num;
	}

	@Override
	public NlsApplyInfoVO queryOnTheWay(NlsApplyInfoVO nlsApplyInfoVo) {
		// TODO Auto-generated method stub
		return null;
	}
/**
	 * 通过授信申请流水号查询同一笔申请下的所有授信申请: <br>
	 *
	 * 0.1:caoxin:2019/5/22 17:56: <br>
	 * @author caoxin
	 * @return
	 * @since 2019/5/22 17:56
	 * @date 2019/5/22 17:56
	 * @version 0.1
	 */
	@Override
	public List<NlsApplyInfoVO> queryListByLmtApplySqe(NlsApplyInfoVO nlsApplyInfoVO) {
		logger.debug("通过授信申请流水号查询列表数据开始......");
		NlsApplyInfo nlsApplyInfo = beanCopy(nlsApplyInfoVO, new NlsApplyInfo());
		List<NlsApplyInfoVO> result = null;
		try {
			List<NlsApplyInfo> list = nlsApplyInfoDao.queryListByLmtApplySqe(nlsApplyInfo);
			pageSet(list, nlsApplyInfo);
			result = (List<NlsApplyInfoVO>) beansCopy(list, NlsApplyInfoVO.class);
		} catch (Exception e) {
			logger.error(CHANGE_DATA_ERROR + e.getMessage());
		}
		logger.debug("通过条件查询列表数据结束----------");
		return result;
	}

	@Override
	public List<NlsApplyInfoVO> queryAllByCurrentDate(NlsApplyInfoVO nlsApplyInfoVO) {
		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<NlsApplyInfo> nlsApplyInfos = nlsApplyInfoDao.queryAllByCurrentDate(nlsApplyInfoVO);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + nlsApplyInfos.size());
		List<NlsApplyInfoVO> list = null;
		try {
			list = (List<NlsApplyInfoVO>) beansCopy(nlsApplyInfos, NlsApplyInfoVO.class);
		} catch (Exception e) {
			logger.error(CHANGE_DATA_ERROR, e);
		}

		return list;
	}

	// 客户经理变更
	@Override
	public boolean batchUpdateCusManage(List<SedSynCusManageVO> sedSynCusManageVOList){
		logger.info("批量更新客户经理开始" );
		boolean result = false;
		try {
			List<SedSynCusManageVO> list = new ArrayList<SedSynCusManageVO>();//
			int num = nlsApplyInfoDao.batchUpdateCusManage(sedSynCusManageVOList);
			result =true;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("客户经理变更更新台账表出错!", e);
		}
		return result;
	}

	@Override
	public NlsApplyInfoVO queryByGobalSerno(String gobalSerno) {
		logger.debug("当前查询参数信息为:" + gobalSerno);
		try {
			NlsApplyInfo queryRslNlsApplyInfo = nlsApplyInfoDao.queryByGobalSerno(gobalSerno);
			if (Objects.nonNull(queryRslNlsApplyInfo)) {
				NlsApplyInfoVO outNlsApplyInfoVo = new NlsApplyInfoVO();
				beanCopy(queryRslNlsApplyInfo, outNlsApplyInfoVo);
				logger.debug("当前查询结果为:" + outNlsApplyInfoVo.toString());
				return outNlsApplyInfoVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	@Override
	public int countInfosByGlobalSerno(String globalSerno) {
		int result = 0;
		logger.debug("根据全局流水号统计信息开始----------全局流水号为："+globalSerno);
		try {
			result = nlsApplyInfoDao.countInfosByGlobalSerno(globalSerno);
		} catch (Exception e) {
			logger.error("根据全局流水号统计信息出现异常-----全局流水号为："+globalSerno, e);
			result = -1;
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<NlsApplyInfoVO> queryAllByLmtApplySeq(String lmtApplySeq) {
		logger.debug("通过授信申请流水号查询列表数据开始......参数为："+ lmtApplySeq);
		List<NlsApplyInfoVO> result = new ArrayList<>();
		try {
			List<NlsApplyInfo> list = nlsApplyInfoDao.queryAllByLmtApplySeq(lmtApplySeq);
			result = (List<NlsApplyInfoVO>) beansCopy(list, NlsApplyInfoVO.class);
		} catch (Exception e) {
			logger.error(CHANGE_DATA_ERROR + e.getMessage());
		}
		logger.debug("通过条件查询列表数据结束----------查询结果总数："+ result.size());
		return result;
	}

	/**
	 * 开发人员：【chenxuan】
	 * 创建/修改日期：【2019/8/8】
	 * 修改描述：根据身份证、证件类型查询借据余额
	 * @Param [accLoanVO]
	 * @return java.math.BigDecimal
	 **/
	@Override
	public BigDecimal queryDuringAmt(NlsApplyInfoVO nlsApplyInfoVo) {
		logger.debug("查询借据余额开始:" + nlsApplyInfoVo.toString());
		try {
			NlsApplyInfoVO nlsApplyInfo = new NlsApplyInfoVO();
			beanCopy(nlsApplyInfoVo,nlsApplyInfo);
			BigDecimal applyAmt = nlsApplyInfoDao.queryDuringAmt(nlsApplyInfo);
			if (applyAmt == null) {
				applyAmt = BigDecimal.ZERO;
			}
			logger.debug(nlsApplyInfoVo.getCertCode()+"查询在途金额结束，在途金额为:" + applyAmt.toString());
			return applyAmt;
		} catch (Exception e) {
			logger.error("查询借据余额发生异常!", e);
		}
		return null;
	}
@Override
	public List<NlsApplyInfoVO> querySampleAllByPk(NlsApplyInfoVO nlsApplyInfoVo) {
		logger.debug("当前查询本人所属数据信息的参数信息为:"+nlsApplyInfoVo);
	NlsApplyInfo nlsApplyInfo = beanCopy(nlsApplyInfoVo,new NlsApplyInfo());
	List<NlsApplyInfoVO> list = null;
	int i = 0;
	try {
//		int listSize = nlsApplyInfoDao.queryCountAll(nlsApplyInfoVo);
		List<NlsApplyInfoVO> rs = nlsApplyInfoDao.queryCountAllCus(nlsApplyInfoVo);
		int d = rs.size()*nlsApplyInfoVo.getPercentage()/100;
		int percentage;
		if(d < 1){
			percentage = 1;
		}else{
			percentage = d;
//			percentage = String.valueOf(i);
		}
		int beginNum = (int)Math.random()*(rs.size() - i + 1);
		int endNum = percentage;
		nlsApplyInfo.setBeginNum(beginNum);
		nlsApplyInfo.setEndNum(endNum);
		nlsApplyInfo.setPageSize(nlsApplyInfoVo.getSize());
		List<NlsApplyInfo> nlsApplyInfos = nlsApplyInfoDao.querySampleAll(nlsApplyInfo);
		logger.debug("当前查询本人所属数据信息的结果集数量为:" + nlsApplyInfos.size());
		list = (List<NlsApplyInfoVO>) beansCopy(nlsApplyInfos, NlsApplyInfoVO.class);
	} catch (Exception e) {
		logger.error(CHANGE_DATA_ERROR + e.getMessage());
	}
	return list;
	}
	@Override
	public List<NlsApplyInfoVO> getFourElementsIfNullList(String prdId) {
		List<NlsApplyInfo> needUploadList = this.nlsApplyInfoDao.getFourElementsIfNullList(prdId);
		try {
			return (List<NlsApplyInfoVO>) beansCopy(needUploadList, NlsApplyInfoVO.class);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("转换数据出错");
		}
		return null;
	}

	@Override
	public int countHsdCustHasUsingLetter(NlsApplyInfoVO nlsApplyInfoVo) {
		logger.debug("根据证件类型、证件号码统计某个客户在途用信申请信息个数开始，入参为："+JSON.toJSONString(nlsApplyInfoVo));
		int res = 0;
		try {
			NlsApplyInfo nlsApplyInfo = beanCopy(nlsApplyInfoVo, new NlsApplyInfo());
			res = nlsApplyInfoDao.countHsdCustHasUsingLetter(nlsApplyInfo);
		} catch (Exception e) {
			logger.error("根据证件类型、证件号码统计某个客户在途用信申请信息个数出现异常!", e);
		} finally {
			logger.debug("根据证件类型、证件号码统计某个客户在途用信申请信息个数结束，个数为：" + res);
		}
		return res;
	}

	@Override
	public List<NlsApplyInfoVO> querySuccessNlsApplyInfo() {
		logger.info("查询成功放款记录");
		try {
			List<NlsApplyInfo> successNlsApplyInfo = this.nlsApplyInfoDao.querySuccessNlsApplyInfo();
			return (List<NlsApplyInfoVO>) beansCopy(successNlsApplyInfo, NlsApplyInfoVO.class);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("查询成功放款记录出错");
		}
		return null;
	}

	@Override
	public List<NlsApplyInfoVO> queryNlsApplyInfoByDnstsAndState(NlsApplyInfoVO nlsApplyInfoVO) {
		logger.info("查询放款状态为成功,用信审批状态为成功的用信申请记录记录");
		try {
			NlsApplyInfo nlsApplyInfo = beanCopy(nlsApplyInfoVO, new NlsApplyInfo());
			List<NlsApplyInfo> successNlsApplyInfo = this.nlsApplyInfoDao.queryNlsApplyInfoByDnstsAndState(nlsApplyInfo);
			return (List<NlsApplyInfoVO>) beansCopy(successNlsApplyInfo, NlsApplyInfoVO.class);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("查询成功放款记录出错");
		}
		return null;
	}

	@Override
	public List<NlsApplyInfoVO> queryByApplySeqs(List<String> list) throws Exception {
		return (List<NlsApplyInfoVO>) beansCopy(this.nlsApplyInfoDao.queryByApplySeqs(list),NlsApplyInfoVO.class);
	}

	@Override
	public List<NlsApplyInfoVO> queryAllByPrdCodes(List<String> prdCodes) {
		logger.debug("通过产品集合查询列表数据开始......");
		List<NlsApplyInfoVO> result = null;
		try {
			List<NlsApplyInfo> list = nlsApplyInfoDao.queryAllByPrdCodes(prdCodes);
			result = (List<NlsApplyInfoVO>) beansCopy(list, NlsApplyInfoVO.class);
			logger.debug("通过条件查询得到的结果集大小为："+result.size());
		} catch (Exception e) {
			logger.error(CHANGE_DATA_ERROR + e.getMessage());
		}
		logger.debug("通过产品集合查询列表数据结束----------");
		return result;
	}

	@Override
	public int queryCountByBdIdPictureState(){
		return this.nlsApplyInfoDao.queryCountByBdIdPictureState();
	}
	@Override
	public int updateStatusByApplySeq(String nlsApplyState, String applySeq) {
		return this.nlsApplyInfoDao.updateStatusByApplySeq(nlsApplyState, applySeq);
	}

	@Override
	public int updateLoanSeqAndDistrNOByApplySeq(String loanSeq, String distrNo, String applySeq) {
		return this.nlsApplyInfoDao.updateLoanSeqAndDistrNOByApplySeq(loanSeq,distrNo,applySeq);
	}

	@Override
	public List<NlsApplyInfoVO> queryNlsApplyInfoByDnstsAndState2() {
		logger.info("查询放款状态为成功,用信审批状态为成功的用信申请记录记录");
		try {
			List<NlsApplyInfo> successNlsApplyInfo = this.nlsApplyInfoDao.queryNlsApplyInfoByDnstsAndState2();
			return (List<NlsApplyInfoVO>) beansCopy(successNlsApplyInfo, NlsApplyInfoVO.class);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("查询成功放款记录出错");
		}
		return null;
	}


	@Override
	public List<NlsApplyInfoVO> queryCusId(String cusId){
		logger.info("根据cusId查询用信表");
		try {
			List<NlsApplyInfo> nlsApplyInfons = this.nlsApplyInfoDao.queryCusId(cusId);
			return (List<NlsApplyInfoVO>) beansCopy(nlsApplyInfons, NlsApplyInfoVO.class);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("查询成功放款记录出错");
		}
		return null;
	}


	@Override
	public List<NlsApplyInfoVO> queryByLmtSeqs(List<String> lmtSeqs) throws Exception {
		return (List<NlsApplyInfoVO>) beansCopy(this.nlsApplyInfoDao.queryByLmtSeqs(lmtSeqs), NlsApplyInfoVO.class);
	}

	@Override
	public int countApplyInfoOnWay(NlsApplyInfoVO nlsApplyInfoVo){
		logger.debug("根据证件类型、证件号码或者用信流水统计某个客户在途用信申请信息个数开始，入参为："+JSON.toJSONString(nlsApplyInfoVo));
		int res = 0;
		try {
			NlsApplyInfo nlsApplyInfo = beanCopy(nlsApplyInfoVo, new NlsApplyInfo());
			res = nlsApplyInfoDao.countApplyInfoOnWay(nlsApplyInfo);
		} catch (Exception e) {
			logger.error("根据证件类型、证件号码或者用信流水统计某个客户在途用信申请信息个数出现异常!", e);
		} finally {
			logger.debug("根据证件类型、证件号码或者用信流水统计某个客户在途用信申请信息个数结束，个数为：" + res);
		}
		return res;
	}
	/**
	*description：
	*param：
	*return:
	*date: 2020/7/30 17:52
	*auther: gcb
	*/
	@Override
	public NlsApplyInfoVO queryLatestNlsApplyInfo(NlsApplyInfoVO nlsApplyInfoVO) {
		logger.info("查询最近一期贷款成功的贷款日期");
		NlsApplyInfo nlsApplyInfo = beanCopy(nlsApplyInfoVO, new NlsApplyInfo());
		NlsApplyInfoVO result = new NlsApplyInfoVO();
		try {
			beanCopy(nlsApplyInfoDao.queryLatestNlsApplyInfo(nlsApplyInfo),result);
		} catch (Exception e) {
			logger.error(CHANGE_DATA_ERROR + e.getMessage());
		}
		return result;
	}

	@Override
	public int queryCountByCertCode(NlsApplyInfoVO nlsApplyInfoVO){
		logger.info("根据证件号、产品id查询数量开始");
		NlsApplyInfo nlsApplyInfo = new NlsApplyInfo();
		nlsApplyInfo.setCertCode(nlsApplyInfoVO.getCertCode());
		nlsApplyInfo.setPrdId(nlsApplyInfoVO.getPrdId());
		int out = nlsApplyInfoDao.queryCountByCertCode(nlsApplyInfo);
		logger.info("查询结果数量为：" + out );
		return out;
	}
	@Override
	public int updateAcctNoByCertCode(NlsApplyInfoVO nlsApplyInfoVO){
		logger.info("根据证件号、产品id更新还款账号开始");
		NlsApplyInfo nlsApplyInfo = new NlsApplyInfo();
		nlsApplyInfo.setCertCode(nlsApplyInfoVO.getCertCode());
		nlsApplyInfo.setPrdId(nlsApplyInfoVO.getPrdId());
		nlsApplyInfo.setRecvAcctNo(nlsApplyInfo.getRecvAcctNo());
		nlsApplyInfo.setRecvBankName(nlsApplyInfo.getRecvBankName());
		int out = nlsApplyInfoDao.updateAcctNoByCertCode(nlsApplyInfo);
		logger.info("更新数据数量为：" + out );
		return out;
	}

	@Override
	public List<NlsApplyInfoVO> queryNlsApplyInfoByDnstsAndState4Yed() {
		logger.info("查询放款状态为成功,用信审批状态为成功的用信申请记录记录");
		try {
			List<NlsApplyInfo> successNlsApplyInfo = this.nlsApplyInfoDao.queryNlsApplyInfoByDnstsAndState4Yed();
			return (List<NlsApplyInfoVO>) beansCopy(successNlsApplyInfo, NlsApplyInfoVO.class);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("查询成功放款记录出错");
		}
		return null;
	}


    @Override
    public NlsApplyInfoVO queryInfoByCertCodeAndApplyDate(NlsApplyInfoVO inNlsApplyInfoVo) {
        logger.debug("当前查询参数信息为:" + inNlsApplyInfoVo);
        try {
            NlsApplyInfo queryNlsApplyInfo = new NlsApplyInfo();
            beanCopy(inNlsApplyInfoVo, queryNlsApplyInfo);
            NlsApplyInfo queryRslNlsApplyInfo = nlsApplyInfoDao.queryInfoByCertCodeAndApplyDate(queryNlsApplyInfo);
            if (Objects.nonNull(queryRslNlsApplyInfo)) {
                NlsApplyInfoVO outNlsApplyInfoVo = new NlsApplyInfoVO();
                beanCopy(queryRslNlsApplyInfo, outNlsApplyInfoVo);
                logger.debug("当前查询结果为:" + outNlsApplyInfoVo.toString());
                return outNlsApplyInfoVo;
            } else {
                logger.debug("当前查询结果为空!");
            }
        } catch (Exception e) {
            logger.error("查询数据发生异常!", e);
        }
        return null;
    }



}
